<template>
    <div class="mainDiv order">
        <div class="rowHead">
            <el-button type="primary" icon="h-document" @click="testChongZhi" v-qx="quanxian.add">账户充值</el-button>
            <el-button type="primary" @click="getList" class="fr ml" icon="h-shuaxin1" style="float:right">刷新列表</el-button>
        </div>
        <el-row :gutter="20" class="rowHead">
            <el-col :span="16" id="el-col-16">
                <el-tag type="success">筛选</el-tag>
                <el-date-picker v-model="page.times" type="daterange" align="right" placeholder="请选择日期范围" :picker-options="pickerOptions2" @change="getList">
                </el-date-picker>
                <el-select v-model="page.zt" @change="getList" class="ml w200" v-qx="quanxian.status">
                    <el-option v-for="(o,index) in zhuanttai" :label="o.txt" :value="o.val" :key="index"></el-option>
                </el-select>
                <el-button type="primary" @click="outXlsx">导出数据</el-button>
            </el-col>
            <el-col :span="8">
                <el-input placeholder="搜索充值单号、客户手机、会员卡号" v-model="page.search" @keyup.enter.native="getList">
                    <i slot="suffix" class="el-icon-search" style="line-height: 40px;width: 25px;cursor: pointer;" @click="getList"></i>
                </el-input>
            </el-col>
        </el-row>
        <el-table :data="rechargelist" :stripe="true" height="100%">
            <!-- <el-table-column prop="id" label="ID" width="80"></el-table-column> -->
            <el-table-column prop="order_sn" label="充值单号" width="170"></el-table-column>
            <el-table-column prop="gname" label="客户">
                <template slot-scope="scope">
                    <span @click="openGuestPage(scope.row.guestid)" style="cursor:pointer;color:#f50">{{scope.row.gname}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="shopname" label="所属分店" show-overflow-tooltip></el-table-column>
            <el-table-column prop="gphone" label="客户手机" width="110"></el-table-column>
            <el-table-column prop="vipcard" label="会员卡号" width="100"></el-table-column>
            <el-table-column prop="recemoney" label="充值金额">
                <template slot-scope="scope">{{scope.row.recemoney+'元'}}</template>
            </el-table-column>
            <el-table-column prop="price" label="赠送金额">
                <template slot-scope="scope">{{scope.row.price?scope.row.price:0}}元</template>
            </el-table-column>
            <el-table-column prop="aname" label="开单人"></el-table-column>
            <el-table-column prop="zname" label="审核人">
                <template slot-scope="scope">{{scope.row.zname?scope.row.zname:'暂无'}}</template>
            </el-table-column>
            <el-table-column prop="status" label="状态">
                <template slot-scope="scope">
                    <Tags :typeValue="scope.row.status" typeName="orderStatus" :tagStyle="{color: '#fff'}"></Tags>
                </template>
            </el-table-column>
            <el-table-column prop="remarks" label="充值描述" show-overflow-tooltip></el-table-column>
            <el-table-column prop="addtime" label="开单日期" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    {{scope.row.addtime|hourTime}}
                </template>
            </el-table-column>
            <el-table-column label="产品操作" width="150" fixed="right">
                <template slot-scope="scope">
                    <el-button-group v-if="scope.row.status==0">
                        <el-tooltip content="去结账" placement="left" :open-delay="800" v-qx="quanxian.jiezhang">
                            <el-button icon="el-icon-h-jiezhang" type="success" size="mini" @click="jiezhang(scope.row)"></el-button>
                        </el-tooltip>
                        <el-button icon="el-icon-delete" type="danger" size="mini" @click="shanchu(scope.row.id)" v-qx="quanxian.del"></el-button>
                    </el-button-group>
                    <el-button-group v-if="scope.row.status==1">
                        <el-tooltip content="去分配" placement="left" :open-delay="800" v-qx="quanxian.jiezhang">
                            <el-button type="success" icon="el-icon-h-bumen" size="mini" @click="jiezhang(scope.row)"></el-button>
                        </el-tooltip>
                        <el-button type="primary" icon="el-icon-h-dayin" size="mini" @click="dayin(scope.row.id)" v-qx="quanxian.print"></el-button>
                    </el-button-group>
                    <el-button-group v-if="scope.row.status==2">
                        <el-button type="info" icon="el-icon-h-chakan" size="mini" @click="jiezhang(scope.row)" v-qx="quanxian.look"></el-button>
                        <el-button type="primary" icon="el-icon-h-dayin" size="mini" @click="dayin(scope.row.id)" v-qx="quanxian.print"></el-button>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>
        <div class="block center bg-light">
            <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="page.pages" :page-sizes="[15, 30, 50, 100]" :page-size="page.limits" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import C from '@/common/common.js'
import P from '@/service/shop/popup.js'
import Guest from '@/service/shop/guest.js'
import Tags from '@/components/orderTags.vue'

export default {
  name: 'rechargeList',
  data () {
    return {
      rechargelist: [],
      total: 0,
      page: {
        pages: 1,
        limits: 15,
        zt: 3,
        search: '',
        times: [new Date(), new Date()]
      },
      zhuanttai: [{
        txt: '请选择单据状态',
        val: 3
      }, {
        txt: '未结账',
        val: 0
      }, {
        txt: '已结账未分配',
        val: 1
      }, {
        txt: '已结账已分配',
        val: 2
      }],
      quanxian: {
        add: 'chongzhi_add',
        status: 'chongzhi_status',
        jiezhang: 'chongzhi_jiezhang',
        del: 'chongzhi_del',
        look: 'chongzhi_look',
        print: 'chongzhi_print'
      },
      pickerOptions2: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const td = new Date()
            td.setHours(0)
            td.setMinutes(0)
            td.setSeconds(0)
            td.setMilliseconds(0)
            const start = new Date()
            start.setTime(td.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, td])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const td = new Date()
            td.setHours(0)
            td.setMinutes(0)
            td.setSeconds(0)
            td.setMilliseconds(0)
            const start = new Date()
            start.setTime(td.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, td])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const td = new Date()
            td.setHours(0)
            td.setMinutes(0)
            td.setSeconds(0)
            td.setMilliseconds(0)
            const start = new Date()
            start.setTime(td.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, td])
          }
        }]
      }
    }
  },
  methods: {
    async testChongZhi () {
      await P.openRechargePopup()
      this.getList()
    },

    // 删除订单
    shanchu: function (id) {
      C.confirm('删除当前订单, 是否继续?', async () => {
        let res = await C.ajax('/shop/recharge/deleterechargeorder', { id: id }, 'post')
        if (res) {
          C.success(res.data)
          this.getList()
        }
      })
    },

    // 结账
    jiezhang: async function (row) {
      await P.openRechargePopup({ orderid: row.id })
      this.getList()
    },

    // 查看当前订单
    chakan: async function (id) {
      await P.openRechargePopup({ orderid: id })
      this.getList()
    },

    // 打印当前订单
    dayin: function (id) {
      C.openBrowserWindow('/printRechargeOrder', { id: id }, { width: 245, height: 600, useSpa: false, spaModel: 'print' })
    },

    sizeChange: function (val) {
      this.page.limits = val
      this.getList()
    },

    currentChange: function (val) {
      this.page.pages = val
      this.getList()
    },

    getList: async function () {
      let url = '/shop/recharge/rechargelist'
      let data = await C.ajax(url, { page: JSON.stringify(this.page) }, 'post')
      if (data) {
        this.rechargelist = data.data
        this.total = data.count
      }
    },

    fanshenhe: async function (id) {
      C.confirm('确定要反结账吗？', async () => {
        let data = await C.ajax('/shop/recharge/fanjiezhang', { 'id': id }, 'post')
        if (data) {
          C.success(data.data)
          this.getList()
        }
      })
    },
    async outXlsx () {
      let header = {
        order_sn: '单号',
        gname: '客户姓名',
        gphone: '客户电话',
        vipcard: '会员卡号',
        recemoney: '充值金额',
        price: '赠送金额',
        addtime: {
          value: '下单时间',
          fun: C.times
        },
        aname: '操作员',
        zname: '审核员'
      }
      let filter = C.copy(this.page)
      filter.limits = 60000
      let data = await C.ajax('/shop/recharge/rechargelist', { page: JSON.stringify(filter) }, 'post')
      let name
      if (filter.times && (filter.times[0] != null && filter.times[1] != null)) {
        name = C.time(Math.round(new Date(filter.times[0]).getTime() / 1000)) + '至' + C.time(Math.round(new Date(filter.times[1]).getTime() / 1000)) + '充值数据'
      } else {
        name = '历史所有充值数据'
      }
      await C.excel(name, header, data.data)
    },
    openGuestPage (id) {
      Guest.openGuestPage(id)
    }
  },
  mounted: function () {
    this.getList()
  },
  components: { Tags }
}
</script>